// import parseTime, formatTime and set to filter
import store from '@/store/index'
export { parseTime, formatTime }
  from '@/utils'
const codeToName = {
  productType: e => {
    return productType(e)
  },
  usageType: e => {
    return supplyType(e)
  },
  planPeriodType: (e) => {
    return planPeriodType(e)
  },
  dailySkipDays: (e) => {
    return dailySkipDays(e)
  },
  dailyPeriod: (e) => {
    return dailyPeriod(e)
  }
}
// 对显示数据转码
export function formateCodeToNameByKey(value, key) {
  if (codeToName[key]) {
    return codeToName[key](value)
  } else {
    return value
  }
}
// 计划周期2个字段合并的- -
export function dailyPeriod(e) {
  const data = e.split(',')

  switch (data[1]) {
    case 0 || '0':
      return data[0] + '天'
      break
    case 1 || '1':
      return data[0] + '周'
      break

    case 2 || '2':
      return data[0] + '月'
      break
    default:
      break
  }
}
// 跳过日期
export function dailySkipDays(e) {
  const data = e.split(',')
  const items = data.map(i => {
    i = '每周' + i
    return i
  })
  return items.join(',')
}

// 计划周期类别
export function planPeriodType(e) {
  switch (e) {
    case 0 || '0':
      return '日常'
      break
    case 1 || '1':
      return '定期'
    default:
      break
  }
  // const location = window.location.href
  // if (location.indexOf('plant-check')) {
  //     //点检周期类型

  // }
  // if (location.indexOf('device-patrol')) {
  //     //巡检周期类型
  // }
  // if (location.indexOf('device-preserve')) {
  //     //保养周期类型
  // }
}

/**
 *
 * @param {*} time
 * @param {*} label
 */

export function formatePreserveType(planPeriodType) {
  switch (planPeriodType) {
    case 0:
      return '例行保养'
      break
    case 1:
      return '故障保养'
      break
    case 2:
      return '定期保养'
      break
    case 3:
      return '停放保养'
      break
    case 4:
      return '走合期保养'
      break
    case 5:
      return '换季保养'
      break
    case 6:
      return '工作转移保养'
      break
    default:
      break
  }
}

/**
 * Show plural label if time is plural number
 * @param {number} time
 * @param {string} label
 * @return {string}
 */
function pluralize(time, label) {
  if (time === 1) {
    return time + label
  }
  return time + label + 's'
}

/**
 * @param {number} time
 */
export function timeAgo(time) {
  const between = Date.now() / 1000 - Number(time)
  if (between < 3600) {
    return pluralize(~~(between / 60), ' minute')
  } else if (between < 86400) {
    return pluralize(~~(between / 3600), ' hour')
  } else {
    return pluralize(~~(between / 86400), ' day')
  }
}

/**
 * Number formatting
 * like 10000 => 10k
 * @param {number} num
 * @param {number} digits
 */
export function numberFormatter(num, digits) {
  const si = [
    { value: 1e18, symbol: 'E' },
    { value: 1e15, symbol: 'P' },
    { value: 1e12, symbol: 'T' },
    { value: 1e9, symbol: 'G' },
    { value: 1e6, symbol: 'M' },
    { value: 1e3, symbol: 'k' }
  ]
  for (let i = 0; i < si.length; i++) {
    if (num >= si[i].value) {
      return (
        (num / si[i].value + 0.1)
          .toFixed(digits)
          .replace(/\.0+$|(\.[0-9]*[1-9])0+$/, '$1') + si[i].symbol
      )
    }
  }
  return num.toString()
}

/**
 * 10000 => "10,000"
 * @param {number} num
 */
export function toThousandFilter(num) {
  return (+num || 0)
    .toString()
    .replace(/^-?\d+/g, m => m.replace(/(?=(?!\b)(\d{3})+$)/g, ','))
}
// 过滤器全局
// 时间转字符串
export function formatDate(value) {
  if (!value || value == '-') return '—'
  const d = new Date(value)
  return d.getFullYear() + '-' + (d.getMonth() + 1) + '-' + d.getDate()
}
// 单据状态转中文

/**
 * 任务状态
 * T_STARTING(21, "进行中"), T_COMPLETE(22, "已完成"),T_TIME_OUT(23, "超时未完成"),T_TRANSFER(24, "转派中"),
/**
 * 计划状态
 *
P_DRAFT(10,"草稿"),
P_WAIT_AUDIT(11,"待审批"),
P_AUDIT_FAIL(12,"审批失败"),
P_START(13,"启用"),
P_STOP(14,"停用"),
P_DELETED(15,"已删除"),
 */
/**
 * 任务项状态
 *I_STARTING(31, "进行中"),
I_COMPLETE(32, "已完成"),
I_TIME_OUT(33, "超时未完成"),
I_TRANSFER(34, "转派中"),
I_WAIT_CONFIRM(35, "待验收"),
I_DEFECT(36, "不合格"),
I_WAIT_ASSIGN(37, "待分配"),
I_WAIT_STARTING(38, "待进行"),
I_HANG(39, "挂起");
 * */

export function formatState(state) {
  switch (state) {
    case 10:
      return '草稿'
      break
    case 11:
      return '待审批'
      break
    case 12:
      return '审批失败'
      break
    case 13:
      return '启用'
      break
    case 14:
      return '停用'
      break
    case 15:
      return '已删除'
      break
    case 21:
      return '进行中'
      break
    case 22:
      return '已完成'
      break
    case 23:
      return '超时未完成'
      break
    case 24:
      return '转派种'
      break
    case 31:
      return '进行中'
      break
    case 32:
      return '已完成'
      break
    case 33:
      return '超时未完成'
      break
    case 34:
      return '转派中'
      break
    case 35:
      return '待验收'
      break
    case 36:
      return '不合格'
      break
    case 37:
      return '待分配'
      break
    case 38:
      return '待进行'
      break
    case 39:
      return '挂起'
      break
    case 40:
      return '超时完成'
      break
    default:
      break
  }
}
// 审批类别转中文
export function formatApproveType(state) {
  switch (state) {
    case 1:
      return '设备台账'
      break
    case 3:
      return '点检计划'
      break
    case 4:
      return '巡检计划'
      break
    case 5:
      return '保养计划'
      break
    case 2:
      return '检定计划'
      break
    case 6:
      return '设备维修'
      break
    case 7:
      return '设备维护'
      break
    default:
      break
  }
}
// 是否非必填
export function formatIsNecessary(state) {
  switch (state) {
    case 0 || '0':
      return '不上传'
      break
    case 1 || '1':
      return '选填'
      break
    case 2 || '2':
      return '必填'
      break
  }
}

export function productType(state) {
  switch (state) {
    case 0:
      return '自制'
      break
    case 1:
      return '外购'
      break
  }
}
// 设备通用/专用
export function supplyType(state) {
  switch (state) {
    case 0:
      return '通用'
      break
    case 1:
      return '专用'
      break
  }
}
// 计划状态
export function planState(state) {
  switch (state) {
    case 0:
      return '草稿'
      break
    case 1:
      return '待审批'
      break
    case 2:
      return '审批失败'
      break
    case 3:
      return '启用'
      break
    case 4:
      return '停用'
      break
    case 5:
      return '已删除'
      break
    default:
      break
  }
}
